package prg420.exercises.mortgage;

import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * This class encapsulates the input data and performs the 
 * mortgage calculation necessary on them.
 * 
 * @author daniel
 */
public class MortgageCalculator
{
  private double P = 0;
  private double i = 0;
  private int N = 0;

  /**
   * This is the constructor and it collects the data for
   * calculation later.
   * 
   * @param P - principal
   * @param i - annual interest rate in fractional form
   * @param N - number of periods (so 10 years monthly is 10 x 12 = 120
   */
  public MortgageCalculator( double P, double i, int N )
  {
    this.P = P;
    this.i = i / 12.0;
    this.N = N;
  }

  /**
   * This is the formula for calculating amortized payments.
   * 
   * @return payment
   */
  public double calculate()
  {
    return P * ( i / (1 - Math.pow( (1 + i), -N ) ) );
  }

}